/*
 * Class used to generate an User structure data
 */
package com.esurvey.core;
import java.util.regex.*;
public class User {
    
    //Attributes
	private int id;
    private String name;
    private String surname;
    private String login;
    private String password;
    private String email;
    private boolean emailVerified;
    private String secretQuestion;
    private String secretAnswer;
    private boolean admin;
    
    //Constructor
    public User(String name, String surname, String login, 
    		String password, String email, String secretQuestion, 
    		String secretAnswer, boolean admin) throws eSurveyException {
    	setName(name);
    	setSurname(surname);
        setLogin(login);
        setPassword(password);
        setEmail(email);
        setEmailVerified(false);
        setSecretQuestion(secretQuestion);
        setSecretAnswer(secretAnswer);
        this.setAdmin(admin);
    }
    
    public User(int id, String name, String surname, String login, 
    		String password, String email, boolean emailVerified,
    		String secretQuestion, String secretAnswer, boolean admin) 
    		throws eSurveyException {
    	this(name, surname, login, password, email, secretQuestion, secretAnswer, admin);
    	setEmailVerified(emailVerified);
    	setId(id);
    }
    
    //Generic methods (Getters & Setters)
    //Getters
    public int getId() { return this.id; }
    public String getName() { return this.name; }
    public String getSecretAnswer() { return this.secretAnswer; }
    public String getSecretQuestion() { return this.secretQuestion; }
    public String getSurname() { return this.surname; }
    public String getLogin() { return this.login; }
    public String getPassword() { return this.password; }
    public String getEmail() { return this.email; }
    public boolean getEmailVerified() { return this.emailVerified; }
    //Setters
    public void setId(int id) {
    	this.id=id;
    }
    public void setName(String name) throws eSurveyException { 
        if (name.length()>=3) {
        	this.name=name;
        } else {
            throw new eSurveyException("Invalid name: "+name);
        }
    }
    public void setSurname(String surname) throws eSurveyException { 
    	if (surname.length()>=3) {
        	this.surname=surname;
        } else {
            throw new eSurveyException("Invalid surname: "+surname);
        }
    }
    public void setLogin(String login) throws eSurveyException { 
        boolean match = Pattern.compile("[a-zA-Z0-9-_]+").matcher(login).matches();
        if (match && login.length()>=3 && login.length()<=12) {
            this.login=login;
        } else {
            throw new eSurveyException("Invalid login: "+login);
        }
    }
    public void setPassword(String password) throws eSurveyException { 
        if (password.length()>=6 && password.length()<=12) {
        	this.password=password;
        } else {
            throw new eSurveyException("Invalid password");
        }
    }
    public void setEmail(String email) throws eSurveyException { 
        boolean match = Pattern.compile("[a-zA-Z0-9-_]+[a-zA-Z0-9-_.]+@[a-zA-Z0-9-]+.[a-z][a-zA-Z0-9]+").matcher(email).matches();
        if (match) {
            this.email=email;
        } else {
            throw new eSurveyException("Invalid eMail: "+email);
        }
    }
    
    public void setEmailVerified(boolean emailVerified) { 
        this.emailVerified=emailVerified;
    }
    public void setSecretQuestion(String secretQuestion) throws eSurveyException { 
        if (secretQuestion.length()>=10) {
        	this.secretQuestion=secretQuestion;
        } else {
            throw new eSurveyException("Invalid secret question: "+secretQuestion);
        }
    }
    public void setSecretAnswer(String secretAnswer) throws eSurveyException { 
        if (secretAnswer.length()>=10) {
        	this.secretAnswer=secretAnswer;
        } else {
            throw new eSurveyException("Invalid secret answer: "+secretAnswer);
        }
    }

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public boolean isAdmin() {
		return admin;
	}
}
